//订单id
var orderId;
//详情
var orderDetail;
//买家信息
var buyerInfo;

//收货人信息
var receiverInfo;

//下单商品详情
var orderGoodsList;

//物流详情
var logistics;


$(function () {
    orderId = getvl("id");
    //根据订单id获取详情
    if (orderId != "") {
        getDetail(orderId);
        getLogistics(orderId);
    }
});

//获取详情的赋值
function assignDetailValue(orderDetail) {

    buyerInfo = orderDetail.muser;

    receiverInfo = orderDetail;

    orderGoodsList = orderDetail.orderGoodsList;

    assignOrderDetail(orderDetail);
    assignBuyerInfo(buyerInfo);
    assignReceiverInfo(receiverInfo);
    assignOrderGoodsList(orderGoodsList);

}

//获取物流详情
function getLogistics() {
    $.ajax({
        url: urlcore + "/api/orders/consultLogsticsAdmin",
        method: "get",
        data: {orderId: orderId},
        async: false,//禁止异步
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        success: function (cbdata) {
            /* logisticsStr = cbdata.data;
             console.log(logisticsStr);
             logistics=JSON.parse(logisticsStr)
             console.log(logistics);*/
            logistics = cbdata.data;
            if (logistics != null) {
                console.log(logistics);
                if (logistics.status != "200" || logistics.data.length == 0) {
                    // alert("暂无物流信息");
                    $("#noLogiInfo").show();
                    return;
                }
                assignLogistics(logistics);
            }

        },
        error: function () {

        }
    });
}

//获取详情
function getDetail(orderId) {
    $.ajax({
        url: urlcore + "/api/orders/getOrderDetailByOrderIdAdmin",
        method: "get",
        data: {orderId: orderId},
        async: false,//禁止异步
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        success: function (cbdata) {
            orderDetail = cbdata.data;
            assignDetailValue(orderDetail);
            console.log(cbdata);
        },
        error: function () {

        }
    });
}

//赋值物流信息
function assignLogistics(logistics) {
    var logiStationList = logistics.data;
    var logiHtml = ""
    for (var index = 0, logiLength = logiStationList.length; index < logiLength; index++) {
        if (index == 0) {
            logiHtml += "<tr class=\"last\">\n";
        } else {
            logiHtml += "<tr>\n";
        }
        logiHtml += "                    <td class=\"row1\"><span class=\"day\">" + logiStationList[index].ftime + "</span></td>\n" +
            "                    <td class=\"status\">&nbsp;<div class=\"col2\"><span class=\"step\"><span class=\"line1\"></span><span\n" +
            "                            class=\"line2\"></span></span></div>\n" +
            "                    </td>\n" +
            "                    <td class=\"context\">" + logiStationList[index].context + "</td>\n" +
            "                </tr>";
    }
    $("#queryResult").html(logiHtml);
}

//赋值订单基本信息
function assignOrderDetail(orderDetail) {
    var gmtDatetime = orderDetail.gmtDatetime;
    var orderType = orderDetail.orderType;
    var orderNumber = orderDetail.orderNumber;
    var sumPrice = orderDetail.totalPrice + orderDetail.couponVal - orderDetail.deliveryMoney;
    var totalPrice = orderDetail.totalPrice;
    var cashPayPrice = orderDetail.cashPayPrice;
    var payType = orderDetail.payType;
    var sourceType = orderDetail.sourceType;
    var couponVal = orderDetail.couponVal;
    var isLogistics = orderDetail.isLogistics;
    var logiNum = orderDetail.logisticsNumber;
    var logiCom = orderDetail.logiCom;
    sourceType = convertSourceType(sourceType);

    orderType = convertOrderType(orderType);

    if (payType != null) {
        if (payType == "3" || payType == "4") {
            assignInput("cashPayPrice", cashPayPrice);
            var payMoney1 = totalPrice - cashPayPrice;
            assignInput("payMoney1", payMoney1);
        }
        if (payType == "1" || payType == "2") {
            var payMoney1 = totalPrice;
            assignInput("payMoney1", payMoney1);
        }
    }

    if(isLogistics!=null){
        var value="";
        if(isLogistics==0){
            value="否";
        }else if(isLogistics==1){
            value="是";
        }
        assignInput("isLogistics", value);
    }

    if (logiNum != undefined && logiNum != null) {
        $("#logiNum").text("物流编号：" + logiNum);
        assignInput("inputLogiNum", logiNum);

    }
    if (logiCom != undefined && logiCom != null) {
        $("#logiCom").text("物流公司：" + logiCom);
    }
    var deliveryMoney = orderDetail.deliveryMoney;
    var orderStatus = orderDetail.orderStatus;
    if (orderStatus != 2)
        $("#btn-shipping").hide();
    if (orderStatus)
        orderStatus = convertOrderStatus(orderStatus, orderType);

    payType = convertPayType(payType);
    var leavingMessage = orderDetail.leavingMessage;

    var merchantRemarks = orderDetail.merchantRemarks;
    var cusServiceRemarks = orderDetail.cusServiceRemarks;


    assignInput("gmtDatetime", gmtDatetime);
    assignInput("orderType", orderType);
    assignInput("orderNumber", orderNumber);
    assignInput("sumPrice", sumPrice);
    assignInput("totalPrice", totalPrice);
    assignInput("payType", payType);
    assignInput("sourceType", sourceType);
    assignInput("couponVal", couponVal);
    assignInput("deliveryMoney", deliveryMoney);

    assignInput("orderStatus", orderStatus);

    assignInput("leavingMessage", leavingMessage);
    assignInput("merchantRemarks", merchantRemarks);
    assignInput("cusServiceRemarks", cusServiceRemarks);


}

//赋值买家信息
function assignBuyerInfo(buyerInfo) {
    var muserName = buyerInfo.userName;
    var muserPhone = buyerInfo.phone;


    assignInput("muserName", muserName);
    assignInput("muserPhone", muserPhone);


}

//赋值收货人信息
function assignReceiverInfo(receiverInfo) {
    var receiverName = receiverInfo.receiverName;
    var receiverPhone = receiverInfo.receiverPhone;

    /*    var receiProvince = receiverInfo.province;
        var city = receiverInfo.city;
        var area = receiverInfo.area;
        var addressDetail = receiverInfo.addressDetail;
        var receiverAddress = receiProvince + " " + city + " " + area + " " + addressDetail;*/
    var receiverAddress = receiverInfo.receiverAddress;

    assignInput("receiverName", receiverName);
    assignInput("receiverPhone", receiverPhone);
    assignInput("receiverAddress", receiverAddress);
}

//赋值订单商品列表信息
function assignOrderGoodsList(orderGoodsList) {
    var liHtml = "";
    for (var ogItem of orderGoodsList) {
        var goodsUrl = ogItem.goods.goodsUrl;
        var goodsName = ogItem.goods.goodsName;
        var sku = ogItem.sku;
        var valuesStr = "";
        if (sku.ifUniformSpec == 1)
            valuesStr = ogItem.sku.valuesStr;
        if (sku.ifUniformSpec == 0)
            valuesStr = "统一规格";
        var skuCount = ogItem.skuCount;

        liHtml += "          <li class=\"list-group-item\">\n" +
            "                <div class=\"row\">\n" +
            "                    <div class=\"col-xs-6 col-md-3 goods-preview\">\n" +
            "                        <img src=\"" + goodsUrl + "\">\n" +
            "                    </div>\n" +
            "                    <div class=\"col-xs-6 col-md-3\">\n" +
            "                        <span style=\"font-size:20px\">" + goodsName + "</span>\n" +
            "                        <br>\n" +
            "                        <br>\n" +
            "                        所选规格：<span>" + valuesStr + "</span>\n" +
            "                        <br>\n" +
            "                        <br>\n" +
            "                        购买数量：<span style=\"font-size:18px\">" + skuCount + "</span>\n" +
            "                    </div>\n" +
            "                </div>\n" +
            "            </li>";
    }
    $("#orderGoods").append(liHtml);
}


/*// 修改订单，发货,optionTag 0 发货
function shippingBatch(orderId) {
    var uptModelList = [];
    if (orderId != undefined) {
        uptModelList.push({
            id: orderId,
            orderStatus: 3
        });
    } else {
        var selection = $table.bootstrapTable('getSelections');
        if (selection.length == 0) {
            alert("请先选择订单");
            return;
        }
        for (var item of selection) {
            uptModelList.push({
                id: item.id,
                orderStatus: 3
            });
        }
    }


    $.ajax({
        type: "POST",
        url: urlcore + "/api/orders/uptOrderBatch",
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(uptModelList),
        success: function (data) {
            alert("订单发货成功！");
            console.log("batchskudata", data);
        },
        error: function () {

        }
    });
}*/

// 修改订单，关闭订单  8 商家取消
function closeOrderBatch() {
    confirm("确认关闭该订单吗？", function (callback) {
        if (callback) {
            closeOrder();
        }
    });

    function closeOrder() {
        var uptModelList = [];
        if (orderId != undefined) {
            uptModelList.push({
                id: orderId,
                orderStatus: 8
            });
        }

        $.ajax({
            type: "POST",
            url: urlcore + "/api/orders/uptOrderBatch",
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(uptModelList),
            success: function (data) {
                alert("订单关闭成功！");
                console.log("batchskudata", data);
            },
            error: function () {

            }
        });
    }

}

//保存备注
function save() {
    var merchantRemarks = $("#merchantRemarks").val();
    var cusServiceRemarks = $("#cusServiceRemarks").val();
    var logisticsNumber = $("#inputLogiNum").val();
    var uptModelList = [];
    if (orderId != undefined) {
        uptModelList.push({
            id: orderId,
            merchantRemarks: merchantRemarks,
            cusServiceRemarks: cusServiceRemarks,
            logisticsNumber: logisticsNumber
        });
    }

    $.ajax({
        type: "POST",
        url: urlcore + "/api/orders/uptOrderBatch",
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(uptModelList),
        success: function (data) {
            alert("保存成功！");
            console.log("batchskudata", data);
            $table.bootstrapTable("refresh");
        },
        error: function () {

        }
    });
}


//input框赋值
function assignInput(domId, value) {
    $('#' + domId).val(value);
}


//转化产品线
function convertSourceType(value) {
    var result;
    if (value == 0) {
        result = "蜜哆哆";
    } else if (value == 1) {
        result = "蜜雅阁";
    }
    return result;
}

//转化支付方式
function convertPayType(value) {
    var result;
    if (value == 1) {
        result = "哆币";
    } else if (value == 2) {
        result = "雅币";
    } else if (value == 3) {
        result = "支付宝";
    } else if (value == 4) {
        result = "微信";
    }
    return result;
}

//转化订单状态 已完成和待评价都是交易完成 6-9都是交易关闭
function convertOrderStatus(value) {
    var result;
    if (value == null || value == undefined)
        return "";
    if (value == 1) {
        result = "待付款";
    } else if (value == 2) {
        result = "未发货";
    } else if (value == 3) {
        result = "已发货";
    } else if (value == 4) {
        result = "已完成";
    } else if (value == 5) {
        result = "待评价";
    } else if (value == 6) {
        result = "自动取消";
    } else if (value == 7) {
        result = "买家取消";
    } else if (value == 8) {
        result = "商家取消";
    } else if (value == 9) {
        if (orderType == 0) {
            result = "维权完成已退款";
        } else if (orderType == 1) {
            result = "拼团失败已退款"
        }
    } else if (value == 10) {
        result = "退款中";
    }
    return result;
}

//订单类型
function convertOrderType(value) {
    var result;
    if (value == null || value == undefined)
        return "";
    if (value == 0) {
        result = "普通订单";
    } else if (value == 1) {
        result = "拼团订单";
    } else if (value == 2) {
        result = "试用众测";
    } else if (value == 3) {
        result = "今日特卖";
    }
    return result;
}